package fairygui.editor.gui.text
{
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import flash.display.BitmapData;
   import flash.display.BitmapDataChannel;
   import flash.geom.ColorTransform;
   import flash.geom.Matrix;
   import flash.geom.Point;
   import flash.geom.Rectangle;
   
   public class EBitmapFont
   {
      
      private static var sHelperRect:Rectangle = new Rectangle();
      
      private static var sHelperPoint:Point = new Point();
      
      private static var sPoint0:Point = new Point();
      
      private static var sHelperTransform:ColorTransform = new ColorTransform();
      
      private static var sHelperMatrix:Matrix = new Matrix();
       
      
      public var packageItem:EPackageItem;
      
      public var atlasItem:EPackageItem;
      
      public var xadvance:int;
      
      public var base:int;
      
      public var size:int;
      
      public var resizable:Boolean;
      
      public var colored:Boolean;
      
      public var lineHeight:int;
      
      private var _dict:Object;
      
      private var _srcId:String;
      
      private var _loading:int;
      
      private var _packed:Boolean;
      
      private var _outlineChannel:int;
      
      private var _ttf:Boolean;
      
      private var _previewChars:String;
      
      public function EBitmapFont()
      {
         super();
      }
      
      public function load() : void
      {
         var content:String = null;
         var lines:Array = null;
         var lineCount:int = 0;
         var i:int = 0;
         var outline:Boolean = false;
         var kv:Object = null;
         var str:String = null;
         var arr:Array = null;
         var j:int = 0;
         var arr2:Array = null;
         var char:String = null;
         var glyph:EBMGlyph = null;
         var pi:EPackageItem = null;
         this._dict = {};
         this.xadvance = 0;
         this.base = 0;
         this.size = 0;
         this.lineHeight = 0;
         this.resizable = false;
         this.colored = false;
         this._previewChars = "";
         try
         {
            content = UtilsFile.loadString(this.packageItem.file);
            if(content == null)
            {
               return;
            }
            lines = content.split("\n");
            lineCount = lines.length;
            kv = {};
            i = 0;
            while(i < lineCount)
            {
               str = lines[i];
               if(str)
               {
                  str = UtilsStr.trim(str);
                  arr = str.split(" ");
                  j = 1;
                  while(j < arr.length)
                  {
                     arr2 = arr[j].split("=");
                     kv[arr2[0]] = arr2[1];
                     j++;
                  }
                  str = arr[0];
                  if(str == "char")
                  {
                     char = String.fromCharCode(kv.id);
                     if(this._previewChars.length < 20)
                     {
                        this._previewChars = this._previewChars + char;
                     }
                     if(this._ttf)
                     {
                        glyph = new EBMGlyph();
                        glyph.imgId = kv.img;
                        glyph.x = kv.x;
                        glyph.y = kv.y;
                        glyph.offsetX = kv.xoffset;
                        glyph.offsetY = kv.yoffset;
                        glyph.width = kv.width;
                        glyph.height = kv.height;
                        glyph.advance = kv.xadvance;
                        glyph.channel = this.translateChannel(kv.chnl);
                        glyph.lineHeight = this.lineHeight;
                        this._dict[char] = glyph;
                     }
                     else
                     {
                        pi = this.packageItem.owner.getItem(kv.img);
                        if(pi && pi.imageInfo != null)
                        {
                           glyph = new EBMGlyph();
                           glyph.imgId = kv.img;
                           glyph.offsetX = kv.xoffset;
                           glyph.offsetY = kv.yoffset;
                           glyph.width = pi.width;
                           glyph.height = pi.height;
                           glyph.advance = kv.xadvance;
                           if(glyph.advance == 0)
                           {
                              if(this.xadvance == 0)
                              {
                                 glyph.advance = glyph.width + glyph.offsetX;
                              }
                              else
                              {
                                 glyph.advance = this.xadvance;
                              }
                           }
                           glyph.lineHeight = glyph.offsetY < 0?int(glyph.height):int(glyph.offsetY + glyph.height);
                           if(this.size > 0 && glyph.lineHeight < this.size)
                           {
                              glyph.lineHeight = this.size;
                           }
                           this._dict[char] = glyph;
                        }
                     }
                  }
                  else if(str == "info")
                  {
                     this._ttf = kv.face != null;
                     this.colored = this._ttf;
                     outline = int(kv.outline) > 0;
                     this.size = int(kv.size);
                     this.resizable = kv.resizable == "true";
                     if(kv.colored != undefined)
                     {
                        this.colored = kv.colored == "true";
                     }
                  }
                  else if(str == "common")
                  {
                     this.lineHeight = kv.lineHeight;
                     if(this.size == 0)
                     {
                        this.size = this.lineHeight;
                     }
                     else if(this.lineHeight == 0)
                     {
                        this.lineHeight = this.size;
                     }
                     this.xadvance = kv.xadvance;
                     this.base = kv.base;
                     this._packed = kv.packed == "1";
                     if(outline)
                     {
                        if(kv.alphaChnl == 1)
                        {
                           this._outlineChannel = BitmapDataChannel.ALPHA;
                        }
                        else if(kv.redChnl == 1)
                        {
                           this._outlineChannel = BitmapDataChannel.RED;
                        }
                        else if(kv.greenChnl == 1)
                        {
                           this._outlineChannel = BitmapDataChannel.GREEN;
                        }
                        else if(kv.blueChnl == 1)
                        {
                           this._outlineChannel = BitmapDataChannel.BLUE;
                        }
                        else
                        {
                           this._outlineChannel = -1;
                        }
                     }
                     else
                     {
                        this._outlineChannel = -1;
                     }
                  }
               }
               i++;
            }
            if(this.size == 0 && glyph)
            {
               this.size = glyph.height;
            }
            if(this.lineHeight == 0)
            {
               this.lineHeight = this.size;
            }
            return;
         }
         catch(err:Error)
         {
            return;
         }
      }
      
      private function translateChannel(param1:int) : int
      {
         switch(param1)
         {
            case 1:
               return BitmapDataChannel.BLUE;
            case 2:
               return BitmapDataChannel.GREEN;
            case 4:
               return BitmapDataChannel.RED;
            case 8:
               return BitmapDataChannel.ALPHA;
            default:
               return 0;
         }
      }
      
      public function dispose() : void
      {
         var _loc1_:EBMGlyph = null;
         if(this._dict != null)
         {
            for each(_loc1_ in this._dict)
            {
               if(_loc1_.bitmapData != null && _loc1_.item == null)
               {
                  _loc1_.bitmapData.dispose();
               }
               if(_loc1_.outlineBitmapData != null)
               {
                  _loc1_.outlineBitmapData.dispose();
               }
            }
            this._dict = null;
         }
         this._loading = 0;
      }
      
      public function get textReady() : Boolean
      {
         return this._loading == 0;
      }
      
      public function getPreviewURL() : String
      {
         var _loc1_:EBMGlyph = null;
         if(this._previewChars.length > 0)
         {
            if(this._ttf)
            {
               return "ui://" + this.packageItem.owner.id + this.packageItem.fontTexture;
            }
            _loc1_ = this._dict[this._previewChars.charAt(0)];
            if(_loc1_)
            {
               return "ui://" + this.packageItem.owner.id + _loc1_.imgId;
            }
            return null;
         }
         return null;
      }
      
      public function prepareCharacters(param1:String) : void
      {
         var _loc2_:int = 0;
         var _loc3_:int = 0;
         var _loc4_:String = null;
         var _loc5_:EBMGlyph = null;
         if(this._dict == null)
         {
            return;
         }
         if(this._ttf)
         {
            if(this.atlasItem == null)
            {
               this.atlasItem = this.packageItem.owner.getItem(this.packageItem.fontTexture);
            }
            if(this.atlasItem != null)
            {
               this._loading++;
               this.atlasItem.owner.getImage(this.atlasItem,this.__imageLoaded2);
            }
         }
         else
         {
            _loc2_ = param1.length;
            _loc3_ = 0;
            while(_loc3_ < _loc2_)
            {
               _loc4_ = param1.charAt(_loc3_);
               _loc5_ = this._dict[_loc4_];
               if(_loc5_)
               {
                  if(_loc5_.imgId && _loc5_.item == null)
                  {
                     _loc5_.item = this.packageItem.owner.getItem(_loc5_.imgId);
                     if(_loc5_.item && !_loc5_.item.data)
                     {
                        this._loading++;
                        _loc5_.item.owner.getImage(_loc5_.item,this.__imageLoaded);
                     }
                  }
               }
               _loc3_++;
            }
         }
      }
      
      private function __imageLoaded(param1:EPackageItem) : void
      {
         this._loading--;
      }
      
      private function __imageLoaded2(param1:EPackageItem) : void
      {
         this._loading--;
      }
      
      public function getGlyph(param1:String) : EBMGlyph
      {
         if(this._dict == null)
         {
            return null;
         }
         return this._dict[param1];
      }
      
      public function draw(param1:BitmapData, param2:EBMGlyph, param3:Point, param4:uint, param5:Number) : void
      {
         var _loc6_:BitmapData = null;
         var _loc7_:BitmapData = null;
         var _loc8_:BitmapData = null;
         param3.x = param3.x + Math.ceil(param2.offsetX * param5);
         param3.y = param3.y + Math.ceil(param2.offsetY * param5);
         param4 = param4 & 16777215;
         if(this._ttf)
         {
            _loc7_ = BitmapData(this.atlasItem.data);
            if(_loc7_ == null)
            {
               return;
            }
            if(param2.bitmapData == null)
            {
               if(param2.width != 0 && param2.height != 0)
               {
                  _loc8_ = new BitmapData(param2.width,param2.height,true,0);
                  sHelperRect.x = param2.x;
                  sHelperRect.y = param2.y;
                  sHelperRect.width = param2.width;
                  sHelperRect.height = param2.height;
                  if(param2.channel == 0)
                  {
                     _loc8_.copyPixels(_loc7_,sHelperRect,sPoint0);
                  }
                  else
                  {
                     _loc8_.fillRect(_loc8_.rect,4294967295);
                     _loc8_.copyChannel(_loc7_,sHelperRect,sPoint0,param2.channel,BitmapDataChannel.ALPHA);
                  }
                  param2.bitmapData = _loc8_;
               }
               else
               {
                  param2.bitmapData = new BitmapData(1,1,true,0);
               }
            }
         }
         else if(param2.bitmapData == null && param2.item != null)
         {
            param2.bitmapData = BitmapData(param2.item.data);
         }
         if(param2.bitmapData != null)
         {
            sHelperMatrix.identity();
            sHelperMatrix.scale(param5,param5);
            sHelperMatrix.translate(param3.x,param3.y);
            sHelperRect.x = param3.x;
            sHelperRect.y = param3.y;
            sHelperRect.width = Math.ceil(param2.width * param5);
            sHelperRect.height = Math.ceil(param2.height * param5);
            if(this.colored)
            {
               sHelperTransform.redMultiplier = (param4 >> 16 & 255) / 255;
               sHelperTransform.greenMultiplier = (param4 >> 8 & 255) / 255;
               sHelperTransform.blueMultiplier = (param4 & 255) / 255;
               param1.draw(param2.bitmapData,sHelperMatrix,sHelperTransform,null,sHelperRect,true);
            }
            else
            {
               param1.draw(param2.bitmapData,sHelperMatrix,null,null,sHelperRect,true);
            }
         }
      }
   }
}
